package com.example.testapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadPic extends AppCompatActivity {
    public static final int GET_DATA_SUCCESS = 1;
    public static final int NETWORK_ERROR = 2;
    public static final int SERVER_ERROR = 3;
    private ImageView ivDownloadPic;

    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GET_DATA_SUCCESS:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    ivDownloadPic.setImageBitmap(bitmap);
                    break;
                case NETWORK_ERROR:
                    Toast.makeText(DownloadPic.this, "图片获取失败", Toast.LENGTH_SHORT).show();
                    break;
                case SERVER_ERROR:
                    Toast.makeText(DownloadPic.this, "网路连接异常", Toast.LENGTH_SHORT).show();
                    break;
                default:break;
            }
            return false;
        }
    });





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_download_pic);

        Button btnDownloadPic = findViewById(R.id.btnDownloadPic);
        Button btnImageLoader = findViewById(R.id.btnImageLoader);
        Button btnPicasso = findViewById(R.id.btnPicasso);
        Button btnFresco = findViewById(R.id.btnFresco);
        Button btnGlide = findViewById(R.id.btnGlide);
        ivDownloadPic = findViewById(R.id.ivDownloadPic);





        btnDownloadPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //图片地址
                String iPath = "http://pic24.nipic.com/20121013/2908050_092109373129_2.jpg";
                setImageUrl(iPath);

            }
        });

        btnImageLoader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageLoader("http://b-ssl.duitang.com/uploads/item/201210/24/20121024114422_ra2Cf.jpeg");
            }
        });

        btnPicasso.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String iPath = "http://b-ssl.duitang.com/uploads/item/201208/30/20120830173930_PBfJE.jpeg";
                Picasso.get().load(iPath).into(ivDownloadPic);
            }
        });

        btnFresco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDraweeView sdv = findViewById(R.id.frescoImage);
                Uri uri = Uri.parse("http://img.52z.com/upload/news/image/20180621/20180621055751_46640.jpg");
                sdv.setImageURI(uri);
            }
        });

        btnGlide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String iPath = "http://b-ssl.duitang.com/uploads/blog/201512/01/20151201131252_XHtis.jpeg";
                Glide.with(DownloadPic.this).load("http://api.91jtg.com:8022/upload/images/default/baike/images/20181031/e6c77f3a-d628-4641-9802-9d81b7792c9b.png").into(ivDownloadPic);

            }
        });
    }


    public void setImageUrl(final String iPath) {
        new Thread() {
            @Override
            public void run () {
                try {
                    //对资源链接
                    URL url = new URL(iPath);
                    //获得连接
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //使用GET
                    connection.setRequestMethod("GET");
                    //超时时间
                    connection.setConnectTimeout(10000);

                    if (connection.getResponseCode() == 200) {
                        //输入流
                        InputStream inputStream = connection.getInputStream();
                        //输入流转为bitmap
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        Message msg = Message.obtain();
                        msg.obj = bitmap;
                        msg.what = 111;
                        handler.sendMessage(msg);
                        msg.what = GET_DATA_SUCCESS;
                        inputStream.close();
                    }
                    else {
                        handler.sendEmptyMessage(SERVER_ERROR);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(NETWORK_ERROR);
                }
            }
        }.start();
    }

    public void imageLoader(String iPath){
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).writeDebugLogs().build();
        ImageLoader.getInstance().init(configuration);
//        ivDownloadPic.setImageBitmap(ImageLoader.getInstance().loadImageSync(iPath));
        ImageLoader.getInstance().loadImage(iPath, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                ivDownloadPic.setImageBitmap(loadedImage);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });
    }
}
